#ifndef IPV4PROPERTYDIALOG_H
#define IPV4PROPERTYDIALOG_H

#include <QDialog>
#include <QLineEdit>
#include <QRadioButton>
#include <QGroupBox>
#include <QPushButton>
#include <QLabel>
#include <QCheckBox>
#include "networkinfo.h"
#include <QComboBox>
#include <QToolTip>
#include <QDebug> // Added for qDebug
#include <QCursor> // Added for QCursor
#include <QMessageBox>
#include <QLabel>
#include <QTimer>

class DnsLineEdit : public QLineEdit {
    Q_OBJECT
public:
    QString dnsTip;
    explicit DnsLineEdit(QWidget *parent = nullptr) : QLineEdit(parent) {
        m_tooltipLabel = nullptr;
        m_tooltipTimer = new QTimer(this);
        m_tooltipTimer->setSingleShot(true);
        connect(m_tooltipTimer, &QTimer::timeout, this, &DnsLineEdit::hideTooltip);
    }
    ~DnsLineEdit() {
        if (m_tooltipLabel) {
            m_tooltipLabel->deleteLater();
        }
    }
protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
    void focusInEvent(QFocusEvent *e) override {
        QLineEdit::focusInEvent(e);
        if (!dnsTip.isEmpty()) {
            showCustomTooltip();
        }
    }
    void focusOutEvent(QFocusEvent *e) override {
        QLineEdit::focusOutEvent(e);
        hideTooltip();
    }
private slots:
    void hideTooltip() {
        if (m_tooltipLabel) {
            m_tooltipLabel->hide();
            m_tooltipLabel->deleteLater();
            m_tooltipLabel = nullptr;
        }
    }
private:
    void showCustomTooltip() {
        hideTooltip();
        m_tooltipLabel = new QLabel(dnsTip);
        m_tooltipLabel->setStyleSheet(
            "QLabel {"
            "background-color: #FFFFCC;"
            "border: 1px solid #CCCCCC;"
            "border-radius: 4px;"
            "padding: 8px;"
            "color: #333333;"
            "font-size: 12px;"
            "}"
        );
        m_tooltipLabel->setWindowFlags(Qt::ToolTip);
        m_tooltipLabel->adjustSize();
        QPoint globalPos = this->mapToGlobal(this->rect().topRight());
        globalPos.setX(globalPos.x() + 10);
        globalPos.setY(globalPos.y() - m_tooltipLabel->height() / 2);
        m_tooltipLabel->move(globalPos);
        m_tooltipLabel->show();
        m_tooltipLabel->raise();
        m_tooltipTimer->start(3000);
    }
    QLabel *m_tooltipLabel;
    QTimer *m_tooltipTimer;
};

class IPv4PropertyDialog : public QDialog {
    Q_OBJECT
public:
    explicit IPv4PropertyDialog(const ConnectionInfo &connInfo, const DeviceInfo &devInfo, QWidget *parent = nullptr);
    void setValues(const QString &ip, const QString &mask, const QString &gw, const QString &dns1, const QString &dns2, bool validate);
    void getValues(QString &ip, QString &mask, QString &gw, QString &dns1, QString &dns2, bool &validate, bool &autoConnect, int &dnsPriorityLevel) const;
    bool isAutoIP() const { return radioAutoIP->isChecked(); }
    bool isAutoDNS() const { return radioAutoDNS->isChecked(); }
    static QString findProfileNameByDevice(const QString &devName);
    void setIpList(const QList<QPair<QString, QString>> &list) { m_ipList = list; }
    QList<QPair<QString, QString>> getIpList() const { return m_ipList; }
    QList<QPair<QString, int>> getGatewayList() const { return m_gatewayList; }
protected:
    bool eventFilter(QObject *obj, QEvent *event) override;
private:
    void loadCurrentSettings();
    bool getAutoConnectStatus(const QString &connectionName);
    void onOkClicked();
    bool validateSettings();
    bool isInSameSubnet(const QString &ip1, const QString &ip2, const QString &mask);
    static bool isValidNetmask(const QString &mask);
    static bool isReservedIP(const QString &ip);
    static bool isNetworkOrBroadcast(const QString &ip, const QString &mask);
    
    ConnectionInfo m_connInfo;
    DeviceInfo m_devInfo;
    QRadioButton *radioAutoIP;
    QRadioButton *radioManualIP;
    QLineEdit *editIP;
    QLineEdit *editMask;
    QLineEdit *editGW;
    QRadioButton *radioAutoDNS;
    QRadioButton *radioManualDNS;
    DnsLineEdit *editDNS1;
    DnsLineEdit *editDNS2;
    QComboBox *comboDnsPriority;
    QCheckBox *checkValidate;
    QPushButton *btnAdvanced;
    QPushButton *btnOK;
    QPushButton *btnCancel;
    QCheckBox *autoConnectCheckBox;
    QList<QPair<QString, QString>> m_ipList;
    QList<QPair<QString, int>> m_gatewayList; // 多网关及metric
};

#endif // IPV4PROPERTYDIALOG_H 